<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller category
 *
 * @author luubk
 * @since Sep 6, 2012 - 2:23:31 PM
 */
class Category extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('m_category');
    }

    function index($offfset = 0, $limit = 10, $noti_del = FALSE)
    {
        $category = $this->m_category->multi_select();

        $this->load->library('pagination');

        $config = array();
        $config['base_url'] = site_url('category/index');
        $config['total_rows'] = count($category);
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $this->set_var('paging', $this->pagination->create_links());
        $this->set_var('items', $this->m_category->multi_select(array(), 'cate_rank, cate_id DESC', $limit, $offfset));        
        $this->set_var('cate_tree', $this->_cate_tree($category));
        if ($noti_del)
            $this->set_var('noti', 'Xóa danh mục thành công!');
        $this->set_var('title', 'Danh sách');
        $this->view('category/index');
    }

    function form($id = FALSE)
    {
        $this->set_var('title', 'Thêm danh mục ứng dụng');
        if ($id)
            $this->set_var('title', 'Sửa danh mục ứng dụng');

        $item = $this->m_category->select_one(array('cate_id' => $id));

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('cate_name', 'Tên nhóm', "required|min_length[5]|max_length[120]");
        if ($id)
            $this->form_validation->set_rules('cate_url', 'URL', 'required');
        else
            $this->form_validation->set_rules('cate_url', 'URL', 'required|callback_check_cate');
        $this->form_validation->set_rules('cate_status', 'Trạng thái', 'required');
        $this->form_validation->set_rules('cate_parent', 'Thư mục cha', 'required');
        $this->form_validation->set_rules('cate_seo', 'SEO', 'required');

        if ($this->input->post())
            if ($this->form_validation->run() == FALSE)
                _jsonout(TRUE, 'custom', validation_errors('<h4 class="alert_error">', '</h4>'));
            else
                $this->_save($id);

        $this->_plugins[] = "jquery_ui/js";
        $this->_plugins[] = "jquery_ui/css/Aristo";
        $this->_plugins[] = "jquery_form";
        $this->_js[] = "upload";

        $this->set_var('item', $item);
        $this->set_var('cates', $this->m_category->multi_select());
        $this->set_var('cate_select_box', $this->_cate_select_box($this->data['cates'], @$item['cate_parent']));
        $this->set_var('id', $id);
        $this->view('category/form');
    }

    private function _save($id = FALSE)
    {
        $data = array();
        $data['cate_name'] = trim($this->input->post('cate_name'));
        $data['cate_desc'] = trim($this->input->post('cate_desc'));
        $data['cate_seo'] = trim($this->input->post('cate_seo'));
        $data['cate_url'] = trim($this->input->post('cate_url'));
        $data['cate_parent'] = intval($this->input->post('cate_parent'));
        $data['cate_status'] = intval($this->input->post('cate_status'));
        $data['cate_rank'] = intval($this->input->post('cate_rank'));
        $data['cate_updated'] = time();

        // Nếu có up ảnh thumbnail
        $data['cate_thumb'] = trim($this->input->post('cate_thumb'));
        if ($this->input->post('thumb') != '')
            $data['cate_thumb'] = $this->_save_photo(array($this->input->post('thumb')), TRUE);

        if ($id)
            if ($this->m_category->update(array('cate_id' => $id), $data))
                _jsonout(TRUE, 'success', "Cập nhật danh mục ứng dụng thành công!");
            else
                _jsonout(FALSE, 'error', "Không cập nhật được danh mục ứng dụng!");

        $data['cate_created'] = time();
        if ($this->m_category->insert($data))
            _jsonout(TRUE, 'success', "Tạo danh mục ứng dụng thành công!");
        else
            _jsonout(FALSE, 'error', "Không tạo được danh mục ứng dụng!");
    }

    public function delete($id = FALSE)
    {
        if ($id)
            $this->m_category->delete(array('cate_id' => $id));

        redirect('category/index/0/10/noti_del');
    }

    private function _cate_select_box($category = array(), $sl_cate = '', $parent = 0, $space = '')
    {
        $tree = '';
        foreach ($category as $cate)
        {
            if ($cate['cate_parent'] == $parent)
            {
                $tree .= "\t<option ";
                $tree .= "value='" . $cate['cate_id'] . "'";
                if ($cate['cate_id'] == $sl_cate)
                    $tree .= ' selected="selected"';
                $tree .= ">";
                $tree .= $space . ' ' . $cate['cate_name'];
                $tree .= "</option>\n";
                $tree .= $this->_cate_select_box($category, $sl_cate, $cate['cate_id'], $space . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
            }
        }
        return $tree;
    }

    private function _cate_tree($category = array(), $display = '', $in_cate = array(), $parent = 0)
    {
        if ($display == 'input')
            $tree = '<ul style="list-style: none">';
        else
            $tree = '<ul>';
        foreach ($category as $cate)
        {
            if ($cate['cate_parent'] == $parent)
            {
                $tree .= '<li>';
                if ($display == 'input')
                {
                    $tree .= "<input type='checkbox' name='app_cate[]' value='" . $cate['cate_id'] . "'";
                    if (in_array($cate['cate_id'], $in_cate))
                        $tree .= ' checked="checked"';
                    $tree .= "/>";
                }
                $tree .= $cate['cate_name'];
                $tree .= '</li>';
                $tree .= $this->_cate_tree($category, $display, $in_cate, $cate['cate_id']);
            }
        }
        $tree .= '</ul>';
        if ($tree == '<ul></ul>')
            return '';
        return $tree;
    }

}

/** 
 * End file category
 */
